package com.tc.controller;

import com.tc.dto.Patient_diseaseDTO;
import com.tc.dto.Patient_diseaseQueryDTO;
import com.tc.service.IPatient_diseaseService;
import com.tc.utils.ResultVo;
import com.tc.vo.PageResultVO;
import com.tc.vo.Patient_diseaseVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  患者疾病关联前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/patient_disease")
@Api(tags = "患者疾病关联管理")
public class Patient_diseaseController {

    @Autowired
    private IPatient_diseaseService patientDiseaseService;

    @ApiOperation(value = "分页查询患者疾病关联")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Patient_diseaseVO>> pageQuery(@RequestBody Patient_diseaseQueryDTO queryDTO) {
        PageResultVO<Patient_diseaseVO> pageResultVO = patientDiseaseService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }
    
    @ApiOperation(value = "根据患者ID查询疾病列表")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/patient/{patientId}")
    public ResultVo<List<Patient_diseaseVO>> getByPatientId(@PathVariable Long patientId) {
        List<Patient_diseaseVO> list = patientDiseaseService.getByPatientId(patientId);
        return ResultVo.ok(list);
    }

    @ApiOperation(value = "根据ID查询患者疾病关联")
    @ApiImplicitParam(name = "id", value = "关联记录ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Patient_diseaseVO> getById(@PathVariable Long id) {
        Patient_diseaseVO vo = patientDiseaseService.getPatientDiseaseById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("记录不存在");
        }
    }

    @ApiOperation(value = "新增患者疾病关联")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Patient_diseaseDTO patientDiseaseDTO) {
        boolean success = patientDiseaseService.addPatientDisease(patientDiseaseDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改患者疾病关联")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Patient_diseaseDTO patientDiseaseDTO) {
        if (patientDiseaseDTO.getId() == null) {
            return ResultVo.fail("ID不能为空");
        }
        
        boolean success = patientDiseaseService.updatePatientDisease(patientDiseaseDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除患者疾病关联")
    @ApiImplicitParam(name = "id", value = "关联记录ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean success = patientDiseaseService.deletePatientDisease(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
}
